package org.gitos.cloud.cmd.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.gitos.cloud.domain.annotation.UnAuth;
import org.gitos.cloud.domain.rpc.LoginRPC;
import org.gitos.cloud.domain.server.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by shizhengchu on 15/12/23.
 */
@RestController
public class LoginController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    HttpServletResponse response;

    @Reference(timeout = 5000)
    LoginRPC loginRPC;

    @RequestMapping("/login/isUserExist")
    @UnAuth
    public Map<String, Object> isUserExist(HttpServletRequest request, @RequestParam(name = "token") String token) {
        return loginRPC.isUserExist(token);
    }

    @RequestMapping("/login/login")
    @UnAuth
    public Map<String, Object> login(HttpServletRequest request, @RequestParam(name = "username") String username, String authCode, @RequestParam(
            name = "password") String password) {
        Map<String, Object> ret = loginRPC.login(username, password, authCode);
        return ret;
    }

    @RequestMapping("/login/sendAuthCode")
    @UnAuth
    public Map<String, Object> sendAuthCode(HttpServletRequest request, @RequestParam(name = "mobile") String mobile) {
        return loginRPC.sendAuthCode(mobile);
    }

    @RequestMapping("/login/register")
    @UnAuth
    public Map<String, Object> register(HttpServletRequest request, @RequestParam(name = "username") String username,
                                        @RequestParam(name = "nickname") String nickname, @RequestParam(name = "authCode") String authCode,
                                        String gender, String headimg, @RequestParam(name = "password") String password) {
        JSONObject jo = new JSONObject();
        jo.put("username", username);
        jo.put("nickname", nickname);
        jo.put("authCode", authCode);
        jo.put("password", password);
        jo.put("headimg", headimg);
        if (gender == null || "".equals(gender)) {
            jo.put("gender", 0);
        } else {
            jo.put("gender", gender);
        }
        return loginRPC.register(jo);
    }

    @RequestMapping("/login/checkLoginState")
    @UnAuth
    public Map<String, Object> checkLoginState(HttpServletRequest request, @RequestParam(name = "uuid") String uuid) {
        return loginRPC.checkLoginState(uuid);
    }

    @RequestMapping("/login/thirdPartyUserRegister")
    @UnAuth
    public Map<String, Object> thirdPartyUserLogin(HttpServletRequest request, String headimg,
                                                   @RequestParam(name = "nickname") String nickname, String gender, @RequestParam(name = "from") String from,
                                                   @RequestParam(name = "openid") String openid) {
        JSONObject jo = new JSONObject();
        jo.put("from", from);
        jo.put("nickname", nickname);
        jo.put("headimg", headimg);
        jo.put("sex", gender);
        jo.put("openid", openid);
        return loginRPC.thirdPartyUserLogin(jo);
    }

    @RequestMapping("/login/generateAuthCode")
    @UnAuth
    public Map<String, Object> generateAuthCode(HttpServletRequest request, @RequestParam(name = "token") String token) {
        // ImageIO.read(new
        // ByteArrayInputStream(loginRPC.generateAuthCode()));
        byte[] generateAuthCode = loginRPC.generateAuthCode(token);
        return generateAuthCode == null ? fail("生成验证码失败") : success(generateAuthCode);
    }

    @RequestMapping("/login/loginOut")
    @UnAuth
    public Map<String, Object> loginOut(HttpServletRequest request, @RequestParam(name = "uuid") String uuid) {
        return loginRPC.loginOut(uuid);
    }

    @RequestMapping("/login/updateUser")
    public Map<String, Object> updateUser(HttpServletRequest request,
                                          @RequestParam(name = "user") String userStr) {
        User user = null;
        try {
            user = JSONArray.parseObject(userStr, User.class);
            if (user.getUuid() == null || "".equals(user.getUuid())) {
                return fail("用户uuid不能为空");
            }
        } catch (Exception e) {
            return fail("参数格式错误");
        }
        return loginRPC.updateUser(user);
    }

}
